package com.healthybear.demo.languagechange

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

/**
 * Data model for RecyclerView items
 */
data class RecyclerItem(
    val titleResId: Int,
    val subtitleResId: Int
)

/**
 * RecyclerView adapter for displaying sample data items with localized text
 * 
 * Requirements addressed:
 * - 5.2: Display RecyclerView with list items
 * - 5.5: Update all content when language is changed
 */
class RecyclerAdapter(
    private val context: Context,
    private val items: List<RecyclerItem>
) : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
    
    /**
     * ViewHolder for RecyclerView items with title and subtitle
     */
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val titleTextView: TextView = view.findViewById(R.id.itemTitle)
        val subtitleTextView: TextView = view.findViewById(R.id.itemSubtitle)
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_recycler, parent, false)
        return ViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]
        
        // Use string resources for proper localization
        holder.titleTextView.text = context.getString(item.titleResId)
        holder.subtitleTextView.text = context.getString(item.subtitleResId)
    }
    
    override fun getItemCount(): Int = items.size
    
    /**
     * Refresh the adapter to update text with new language
     * Call this method when language changes to update all visible items
     * Requirement 5.5: Update all content when language is changed
     */
    fun refreshLanguage() {
        notifyDataSetChanged()
    }
    
    companion object {
        /**
         * Generate sample data list for testing language switching
         * Uses string resource IDs for proper localization
         */
        fun createSampleData(): List<RecyclerItem> {
            return listOf(
                RecyclerItem(R.string.recycler_item_1, R.string.recycler_subtitle_1),
                RecyclerItem(R.string.recycler_item_2, R.string.recycler_subtitle_2),
                RecyclerItem(R.string.recycler_item_3, R.string.recycler_subtitle_3),
                RecyclerItem(R.string.recycler_item_4, R.string.recycler_subtitle_4),
                RecyclerItem(R.string.recycler_item_5, R.string.recycler_subtitle_5)
            )
        }
    }
}